package com.hehe.httpurlconnectiontest2;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.LinkedList;

/**
 * Created by lenove on 2017/11/11.
 *
 * 显示设备列表的Adapter
 */

public class DeviceBaseAdapter extends BaseAdapter {

    //TODO 这里感觉应该使用Arraylist，之后再改吧
    LinkedList<YeelinkDecives> data = null;
    Context mContext;

    DeviceBaseAdapter(Context mContext,LinkedList data){
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public int getCount() {
        if (data == null){
            return 0;
        }else{
            return data.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = new ViewHolder();
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_device,parent,false);
            holder.DeviceName = (TextView)convertView.findViewById(R.id.DeviceName);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.DeviceName.setText(data.get(position).getDevice_title());

        return convertView;
    }

    //刷新页面
    public void flashList(){
        notifyDataSetChanged();
    }

    private static class ViewHolder{
        TextView DeviceName;
    }

    //通过一个新的数组更新数据
    public void flashData(LinkedList<YeelinkDecives> list){
        data = list;
        notifyDataSetChanged();
    }

    //在末尾增加一个数据
    public void appDataEnd(YeelinkDecives decives){
        if (data == null){
            data = new LinkedList<>();
            data.addLast(decives);
        }else {
            data.addLast(decives);
        }
        notifyDataSetChanged();
    }

    //获取指定位置的设备ID(不是设备名！)
    public String getDeviceID(int position){
        return data.get(position).getDevice_id();
    }

    //获取指定位置设备的传感器
    public LinkedList<YeelinkSensor> getSensorsList(int position){
        return data.get(position).getSensorLinkedList();
    }

    public LinkedList<YeelinkDecives> getData() {
        return data;
    }
}
